package kr.zetaomicron.ultrasolitaire;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import kr.zetaomicron.framework.Screen;
import kr.zetaomicron.framework.impl.GLGame;

// Base activity class.
public class StartHere extends GLGame {
  private static final int MENU_NEW_GAME  = 1;
  private static final int MENU_RESTART   = 2;
  private static final int MENU_OPTIONS   = 3;
  private static final int MENU_SAVE_QUIT = 4;
  private static final int MENU_QUIT      = 5;
  private static final int MENU_KLONDIKE = 6;
  private static final int MENU_SPIDER    = 7;
  private static final int MENU_FREECELL  = 8;
  private static final int MENU_FORTYTHIEVES = 9;
  private static final int MENU_STATS     = 10;
  private static final int MENU_HELP      = 11;

  // View extracted from main.xml.
  private View mMainView;
  private World mSolitaireView;
  private SharedPreferences mSettings;

  private boolean mDoSave;
  
	// Shared preferences are where the various user settings are stored.
	public SharedPreferences GetSettings() {	return mSettings;	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	
	    SubMenu subMenu = menu.addSubMenu(0, MENU_NEW_GAME, 0, "New Game");
	    subMenu.add(0, MENU_KLONDIKE, 0, "Klondike");
	    subMenu.add(0, MENU_SPIDER, 0, "Spider");
	    subMenu.add(0, MENU_FREECELL, 0, "FreeCell");
	    subMenu.add(0, MENU_FORTYTHIEVES, 0, "Forty Thieves");
	
	    menu.add(0, MENU_RESTART, 0, "Restart");
	    menu.add(0, MENU_OPTIONS, 0, "Options");
	    menu.add(0, MENU_SAVE_QUIT, 0, "Save & Quit");
	    menu.add(0, MENU_QUIT, 0, "Quit");
		menu.add(0, MENU_STATS, 0, "Stats");
		menu.add(0, MENU_HELP, 0, "Help");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_KLONDIKE:
			Settings.tmpGameSettings = true;
			//DisplayGameSettings();
			//mSolitaireView.InitGame(Rules.KLONDIKE);
			break;
		case MENU_SPIDER:
			Settings.tmpGameSettings = false;
        //mSolitaireView.InitGame(Rules.SPIDER);
			break;
		case MENU_FREECELL:
			//mSolitaireView.InitGame(Rules.FREECELL);
			break;
		case MENU_FORTYTHIEVES:
			//mSolitaireView.InitGame(Rules.FORTYTHIEVES);
			break;
		case MENU_RESTART:
			//mSolitaireView.RestartGame();
			break;
		case MENU_STATS:
			//DisplayStats();
			break;
		case MENU_OPTIONS:
			//DisplayOptions();
			break;
		case MENU_HELP:
			//mSolitaireView.DisplayHelp();
			break;
		case MENU_SAVE_QUIT:
			//mSolitaireView.SaveGame();
			mDoSave = false;
			finish();
			break;
		case MENU_QUIT:
			mDoSave = false;
			finish();
			break;
		}

		return false;
	}

	@Override
	public void onPause() {
		super.onPause();
//  	if(Settings.soundEnabled)
//  	Assets.music.pause();
	}

//	@Override
//	protected void onStop() {
//		super.onStop();
//		if (mDoSave) {
//			//mSolitaireView.SaveGame();
//		}
//	}

	@Override
	public void onResume() {
		super.onResume();
		//mSolitaireView.onResume();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

//	public void DisplayOptions() {
//		mSolitaireView.SetTimePassing(false);
//		new Options(this, mSolitaireView.GetDrawMaster());
//	}

//  public void DisplayStats() {
//    mSolitaireView.SetTimePassing(false);
//    //new Stats(this, mSolitaireView);
//  }
//
//  public void CancelOptions() {
//    setContentView(mMainView);
//    mSolitaireView.requestFocus();
//    mSolitaireView.SetTimePassing(true);
//  }
//
//  public void NewOptions() {
//    setContentView(mMainView);
//    //mSolitaireView.InitGame(mSettings.getInt("LastType", Rules.SOLITAIRE));
//  }
//
//  // This is called for option changes that require a refresh, but not a new game
//  public void RefreshOptions() {
//    setContentView(mMainView);
//    mSolitaireView.RefreshOptions();
//  }
  
	boolean firstTimeCreate = true;

	public Screen getStartScreen() {
		return new GameScreen(this);
	}

	@Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {         
        super.onSurfaceCreated(gl, config);
        if (firstTimeCreate) {
            Settings.load(getFileIO());
            Assets.load(this);
            firstTimeCreate = false;
        } else {
            Assets.reload();
        }
    }
}
